package com.pet.server.ext.action.mall.product;

import java.util.ArrayList;
import java.util.List;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.constants.Status.Source;
import com.pet.server.ext.data.Campaign;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.service.SystemService;
import com.pet.server.ext.service.TbkService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.cache.ConstantUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 专区
 * */
public class Region extends ActionBase{

	private String pet_code,cate_code,sort;
	
	public Region(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		pet_code=params.getFieldValue("pet_code");
		cate_code=params.getFieldValue("cate_code");
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		SystemService systemService = new SystemService();
		List<Constant> list1 = new ArrayList<>();
		
		String code="maogou";
		
		if(!StringUtil.isEmpty(cate_code)){
			if("001001".equals(cate_code)){
				code="shipin";
			}
			if("001002".equals(cate_code)){
				code="yongpin";
			}
		    list1=systemService.queryConstant(cate_code);
		}else{
			list1=systemService.queryConstant("001001");
			list1.addAll(systemService.queryConstant("001002"));
            
		}
		
		
        List<Campaign> banner = userService.queryCampaign(BaseAttribute.pay_type.banner.toString(), 3);
		
		JSONObject o = new JSONObject();
        JSONArray bannerJson = new JSONArray();
		for(Campaign cam:banner){
			JSONObject co = new JSONObject();
	 		co.put("click_type", cam.getClick_type());
	 		co.put("click_url", cam.getClick_url());
	 		co.put("text", cam.getText());
	 		co.put("img_url", cam.getImg_url());
	 		co.put("targt", cam.getTarget());
	 		co.put("param", StringUtil.dealEmpty(cam.getParam(), null));
	 		
	 		
	 		
	 		
            bannerJson.add(co);
		}
		//焦点图
		o.put("banner", bannerJson);
        
		//System.out.println(list1.size()+"=====================================");
		
		List<Constant> flist=filterList(list1, code);
		//数据加载...
		JSONArray listJson = parseArray(flist);
		
		o.put("list", listJson);
		
		Result result = new Result();
		result.put("data",o);
		return result;
	}
	
	
	private List<Constant> filterList(List<Constant> list1,String code){
		List<Constant> list = new ArrayList<>();
		for(Constant cont:list1){
			System.out.println(cont.getVal()+"==================");
			
			if(!StringUtil.isEmpty(cont.getVal())&&code.matches(cont.getVal())){
                 list.add(cont);				
			}
		}
		
		return list;
	}
	
	
	
	private JSONArray parseArray(List<Constant> list) throws ApiError{
		TbkService tbkService = new TbkService();
		JSONArray listJson = new JSONArray();
		for(Constant cont:list){
			
			//System.out.println(cont.sort+"  ================================================");
			
			//if(!StringUtil.isEmpty(cont.sort)&&cont.sort>=90){
				JSONObject so = new JSONObject();
	        	so.put("category_name", cont.conts_name);
	        	so.put("category_type", cont.code);
	        	List<ProductInfo> products = tbkService.queryTbkProductInfo(pet_code, cont.code, null, null, null, sort, 10);
	        	JSONArray productJson = new JSONArray();
	    		for(ProductInfo p:products){
	    			JSONObject po = new JSONObject();
	    			po.put("ico_path", p.ico_path);
	    			po.put("id", p.id);
	    			po.put("pet_cate", p.pet_cate);
	    			po.put("product_cate", p.product_cate);
	    			po.put("product_href", p.product_href);
	    			po.put("product_name", p.product_name);
	    			po.put("product_price", p.product_price);
	    			po.put("sale_price", p.sale_price);
	    			if(!StringUtil.isEmpty(p.images)&&!"null".equals(p.images)){
	    				//po.put("images", p.images);
	    			}
	    			po.put("source", Source.tmall.toString());
	    			po.put("num_iid", p.num_iid);
	    			po.put("source", Source.tmall.toString());
	    			productJson.add(po);
	    		}
	    		so.put("products",productJson);
	    		listJson.add(so);
			//}
			
			
		}
		return listJson;
	}
	

	
	
	
	
	
	
}
